$(function () {
    selectUser();
    $("#submitBtn").addClass("disable");
    /*发送验证码*/
    $("#send_code").click(function(){
        verification_Null()
        if(flag){
            send_code();
        }
    });

    //年月选择器
    laydate.render({ 
        elem: '#bankExpireDate',
        type: 'month',
        format: 'yyyy/MM',
        change: function(value){
            $('#bankExpireDate').val(value);
            $('#layui-laydate1').remove();//删除
        }
    });

    initEvent() 
});

var flag=true;
/*验证码*/
var wait = 60;
function countdown(obj, msg) {
    obj = $(obj);
    if (wait <= 0) {
        obj.removeClass("disabled");
        obj.prop("disabled", false);
        obj.html(msg);
        wait = 60;
    } else {
        if (msg == undefined || msg == null) {
            msg = obj.html();
        }
        obj.addClass("disabled");
        obj.prop("disabled", true);
        obj.html(wait + "秒后重新获取");
        wait--;
        setTimeout(function() {
            countdown(obj, msg)
        }, 1000)
    }
}
    
/*隐藏信用卡类型*/
function hideBankMask(){
    $(".bank-mask").hide();
    $(".bank-layer").animate({"bottom":"-100%"})
}
/*显示信用卡类型*/
function showBankMask(){
    $(".bank-mask").show();
    $(".bank-layer").animate({"bottom":"0"})
}


/*绑定的银行卡id*/
var u_bank_id = null;
var applyId = null;

/*提交信息获取验证码----绑卡---信用卡绑定：Secoo@bindCreditCards*/
function send_code() {
    var bankTel = $("#bankTel").val();/*银行预留手机号*/
    var bankCode = $("#bankCode").val();/*信用卡类型--银行编码*/
    var bankNo = $("#bankNo").val();/*银行信用卡号*/
    var bankExpireDate = $("#bankExpireDate").val();/*有效期*/
    var bankCvn2 = $("#bankCvn2").val();/*银行卡cv2  安全码*/
    var cardName = $("#creditCard").val();/*信用卡类型--名称*/
    
    var datas = {
        interface:'Secoo@bindCreditCards', 
        bankTel: bankTel,/*银行预留手机号*/
        bankCode:bankCode,/*信用卡类型--银行编码*/
        bankNo: bankNo,/*银行信用卡号*/
        bankExpireDate: bankExpireDate,/*有效期*/
        bankCvn2: bankCvn2,/*银行卡cv2  安全码*/
        cardName: cardName,/*信用卡类型--名称*/
    };

    common.ajax({
        data: datas,
        success: function (data) {
            if (data.status == '2000') {
                localStorage.setItem("bankTel",bankTel);
                applyId = data.result.applyId;
                u_bank_id = data.result.ub_id;

                countdown("#send_code","获取验证码");
                //falert(data.msg);
                $("#submitBtn").removeClass("disable");
                /*提交*/
                $("#submitBtn").click(function(){
                    var phone_code = $("#code").val();/*验证码*/
                    if(phone_code == ""){
                        falert("请输入验证码");
                    }else{
                        btn_Submit();
                    }
                });

            } else if (data.status == '5000') {
                falert(data.msg);
            }
        }
    });
}

/*提交---立即支付--信用卡绑定验证：Secoo@bindCreditCardsVerification*/
function btn_Submit() {
    var phone_code = $("#code").val();/*验证码*/
    var pay_agree = $("#payAgree");/*是否同意信用卡账期协议*/
    if(!pay_agree.is(":checked")){
        falert("请先同意《信用卡账单协议》");
        return false;
    }
    localStorage.setItem("user_bank_id",u_bank_id);
    var datas = {
        interface:'Secoo@bindCreditCardsVerification', 
        user_bank_id:u_bank_id,/*信用卡ID*/
        applyId: applyId,/*发送激活短信返回值*/
        phone_code:phone_code,/*验证码*/
    };
    common.ajax({
        data: datas,
        success: function (data) {
            if (data.status == '2000') {
                //console.log(data)
                window.location.href="/secoo/pay_card";
                /*绑定的银行卡id*/
            } else if (data.status == '5000') {
                falert(data.msg);
            }
        }
    });
}

/*信用卡列表*/
function initEvent() {  
    var term = localStorage.getItem("b_period");
    var datas = {
        interface:'Secoo@bankList',       
        term:term,     
    };

    common.ajax({
        data: datas,
        success: function (data) {
            if (data.status == '2000') {   
                var list = data.result;         
                var html = '<ul>'
                for(var i= 0;i<list.length;i++){
                    var item = list[i];
                    html += '<li data-type="'+item.bankCode+'">'+item.bankName+'</li>'
                }
                html += '</ul>'
                $(".bank-layer").append(html);
                /*选择信用卡类型*/
                $(".bank-layer li").click(function(){
                    var txt = $(this).html();
                    var dateType = $(this).attr("data-type");
                    $("#creditCard").val(txt)
                    $("#bankCode").val(dateType)
                    $(this).addClass("cur").siblings().removeClass("cur");
                    hideBankMask()
                });

            } else if (data.status == '5000') {
                falert(data.msg);
            }
        }
    });  
}

/*查询用户信息*/
function selectUser() {  
    var datas = {
        interface:'Secoo@getUserInfo',    
    };

    common.ajax({
        data: datas,
        success: function (data) {
            if (data.status == '2000') { 
                $("#userName").val(data.result.u_true_name);
                $("#idCardNo").val(data.result.u_ident_no);
            } else if (data.status == '5000') {
                $("#userName").val().attr("readonly",false);
                $("#idCardNo").val().attr("readonly",false);
            }
        }
    });  
}

/*验证是否为空*/
function verification_Null(){
    var userName = _trim($("#userName").val());/*用户姓名*/
    var idCardNo = _trim($("#idCardNo").val());/*用户身份证号*/
    var bankTel = _trim($("#bankTel").val());/*银行预留手机号*/

    var bankCode = _trim($("#bankCode").val());/*信用卡类型--银行编码*/
    var bankNo = _trim($("#bankNo").val());/*银行信用卡号*/
    var bankExpireDate = _trim($("#bankExpireDate").val());/*有效期*/
    var bankCvn2 = _trim($("#bankCvn2").val());/*银行卡cv2  安全码*/

    if(bankNo == ''){
        falert("请输入本人信用卡号");
        flag=false;
        return false;
    }else{
        flag = true
    }

    if(bankCode == ''){
        falert("请选择卡类型");
        flag=false;
        return false;
    }else{
        flag = true
    }

    if(bankExpireDate == ''){
        falert("请输入有效期");
        flag=false;
        return false;
    }else{
        flag = true
    }

    if(bankCvn2 == ''){
        falert("请输入安全码");
        flag=false;
        return false;
    }else{
        flag = true
    }

    if(userName == ''){
        falert("请输入姓名");
        flag=false;
        return false;
    }else{
        flag = true
    }

    if(idCardNo == ''){
        falert("请输入身份证号");
        flag=false;
        return false;
    }else{
        flag = true
    }

    if(bankTel == ''){
        falert("请输入手机号");
        flag=false;
        return false;
    }else if(!Regexs.allphpne.test(bankTel)){ /*手机号正则校验*/
        falert("请输入正确的手机号码");  
        flag=false;
        return false; 
    }else{
        flag = true
    }
}
/**
 * 前后去除空格
 */
function _trim(text){
    return text.replace(/(^\s*)|(\s*$)/g, "");
}

/**
 * 正则校验
 */
var Regexs = {
    email: (/^[0-9a-z][0-9a-z\-\_\.]+@([0-9a-z][0-9a-z\-]*\.)+[a-z]{2,}$/i),//邮箱  
    phone: (/^0[0-9]{2,3}[2-9][0-9]{6,7}$/),//座机手机号码  
    ydphpne: (/^((13[4-9])|(15[012789])|147|182|187|188)[0-9]{8}$/),//移动手机号码  
    //allphpne: (/^((13[0-9])|(15[0-9])|(18[0-9]))[0-9]{8}$/),//所有手机号码  
    allphpne: (/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[0-9])\d{8}$/),//所有手机号码  新
    ltphpne: (/^((13[0-2])|(15[56])|(186)|(145))[0-9]{8}$/),//联通手机号码  
    dxphpne: (/^((133)|(153)|(180)|(189))[0-9]{8}$/),//电信手机号码  
    url: (/^http:\/\/([0-9a-z][0-9a-z\-]*\.)+[a-z]{2,}(:\d+)?\/[0-9a-z%\-_\/\.]+/i),//网址  
    num: (/[^0-9]/),//数字  
    cnum: (/[^0-9a-zA-Z_.-]/),  
    photo: (/\.jpg$|\.jpeg$|\.gif$|\.png$|\.bmp$/i),//图片格式  
    row: (/\n/ig),
    invalidChar:(/^(([^\^\.<>%&',;=?$"':#@!~\]\[{}\\/`\|])*)$/),
    idCard:(/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/),//身份证号
    price:(/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/)//金额
};

 //查看信用卡账单协议
function viewAgree(){    
    $(".shadow-div").css("display","block");
    $(".shadow-content").css("display","block"); 
    $.get("/agreement/bank_bill",function(d){
        $(".contract-info").html(d);
    });   
} 
//关闭信用卡账单协议
function closeAgree(){
    $(".shadow-div").css("display","none");
    $(".shadow-content").css("display","none");   
}